(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.process.processConfig.createRejectController', createRejectController);
	createRejectController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'processConfigService', 'ajaxServiceFactory' ];
	function createRejectController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, processConfigService, ajaxServiceFactory) {
		var ctrl = this, processConfigInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.processConfig = {};
		
		ctrl.init = function() {
			var params = {
					taskId : processConfigInfo.taskId
			};
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.variablesCfg.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.processConfig.variables';
			authService.createReject(params).then(function(response) {
				var parm = {};
				parm.serviceCode = response.data.model.process.serviceCode;
				processConfigService.getBpmprocContextOptions(parm).then(function(resOption) {
					ctrl.bpmprocContextOptions = resOption;
					ctrl.bpmprocContextOptions.hasEmptyOption = true;
					ctrl.processConfig = response.data.model;
					ctrl.processConfig.taskId = response.data.task.taskId || processConfigInfo.taskId;
					ctrl.processConfig.variables = ctrl.processConfig.variables || [];
				});
			});
		};
		

		ctrl.companyCodeChanged = function() {
			ctrl.processConfig.companyCode = ctrl.processConfig.companyCode;
			if ( !ctrl.processConfig.companyCode ) {
				ctrl.processConfig.companyCode = '*';
				ctrl.processConfig.companyName = '*';
			}
		};
		
		ctrl.companyCpCodeChanged = function() {
			ctrl.processConfig.companyCpCode = ctrl.processConfig.companyCpCode;
			if ( !ctrl.processConfig.companyCpCode ) {
				ctrl.processConfig.companyCpCode = '*';
				ctrl.processConfig.companyCpName = '*';
			}
		};
		
		ctrl.createRejectSave = function() {
			return authService.createRejectSave(ctrl.processConfig).then(saveSuccessful, saveFailed);
		};

		ctrl.createRejectSubmit = function() {
			return authService.createRejectSubmit(ctrl.processConfig).then(submitSuccessful, submitFailed);
		};
		
		ctrl.createRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.processConfig.process.wfprocName]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createRejectCancel(ctrl.processConfig).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedVariables = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.process.list',
					'state' : 'cda.process.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.merge(ctrl.processConfig, response.data.model);
			} else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.process.list',
					'state' : 'cda.process.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

	}
})(angular);
